package com.hoperun.product.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import android.os.Environment;
import android.util.Xml;

public class Setting {
	// 1 公司服务器， 2 宁波服务器 3 越南服务器
	public static int TYPE = 1;

	// private static String URL =
	// "http://192.168.136.11:8899/yherp/PublicWebServiceInterface";
	// private static String URL =
	// "http://192.168.1.37:8899/yherp/PublicWebServiceInterface"; // 宁波
	// private static String URL =
	// "http://10.10.122.4:8008/yherp/PublicWebServiceInterface"; // 越南

	private static String BaseURL = "";
	private static String UpdateXmlURL = "";

	public final static String configName = "config.xml";

	public final static String downloadFileDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Product/Download/";

	public final static String configFileDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Product/";

	// public final static String xmlName = "/update.xml";

	public static String OPT_FLAG = "OPT_FLAG";
	public static String MSG_INFO = "MSG_INFO";

	// 后台问题可能存在超时10秒的情况，需要延长一下

	private static int SOCKET_TIMEOUT = 60000;

	private static int REQUEST_RATE = 10000;
	private static int FRESH_PAGE_RATE = 10000;

	public static String TYPE_CP = "0";
	public static String TYPE_XB = "1";

	public static String mCurrentTM = "";
	public static String mCurrentHYH = "";
	public static String mCurrentHH = "";

	public static String getUrl() {
		return BaseURL;
	}

	public static int getSocketTimeout() {
		return SOCKET_TIMEOUT;
	}

	public static int getRequestRate() {
		return REQUEST_RATE;
	}

	public static int getPageRate() {
		return FRESH_PAGE_RATE;
	}

	private static void setUrl(String url) {
		BaseURL = url;
	}

	/**
	 * @return the updateXmlURL
	 */
	public static String getUpdateXmlURL() {
		return UpdateXmlURL;
	}

	/**
	 * @param updateXmlURL
	 *            the updateXmlURL to set
	 */
	public static void setUpdateXmlURL(String updateXmlURL) {
		UpdateXmlURL = updateXmlURL;
	}

	public static void parseXml(String configPath) {

		try {
			FileInputStream in = new FileInputStream(configPath);
			XmlPullParser parser = Xml.newPullParser();
			parser.setInput(in, "utf-8");
			int eventType = parser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {

				if (eventType == XmlPullParser.START_TAG) {
					String name = parser.getName();
					if (name.equalsIgnoreCase("baseurl_test")) {
						if (1 == TYPE) {
							setUrl(parser.nextText());
						}

					} else if (name.equalsIgnoreCase("updateurl_test")) {
						if (1 == TYPE) {
							setUpdateXmlURL(parser.nextText());
						}
					} else if (name.equalsIgnoreCase("baseurl_ningbo")) {
						if (2 == TYPE) {
							setUrl(parser.nextText());
						}

					} else if (name.equalsIgnoreCase("updateurl_ningbo")) {
						if (2 == TYPE) {
							setUpdateXmlURL(parser.nextText());
						}
					} else if (name.equalsIgnoreCase("baseurl_yuenan")) {
						if (3 == TYPE) {
							setUrl(parser.nextText());
						}
					} else if (name.equalsIgnoreCase("updateurl_yuenan")) {
						if (3 == TYPE) {
							setUpdateXmlURL(parser.nextText());
						}
					}
				}
				eventType = parser.next();
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static String xgcgZh = "修改成功";
	private static String zbdbmxxZh = "找不到对应的部门信息";

	private static String xgcgVi = "thao tac thanh cong!";
	private static String zbdbmxxVi = "Nhan thong tin cong nhan that bai";

	public static String getMsgInfo(Context mContex, String msg) {
		SharedPreferences sharedoPreferences = mContex.getSharedPreferences("data", Context.MODE_PRIVATE);
		String yy = sharedoPreferences.getString("yy", "");

		if (yy != null && yy.equals("vi") && msg != null) {
			if (msg.trim().contains(xgcgZh)) {
				msg = xgcgVi;
			}

			if (msg.trim().contains(zbdbmxxZh)) {
				msg = zbdbmxxVi;
			}
		}
		return msg;
	}

	public static String getVersionName(Context context) {
		String versionName = "";
		try {
			PackageManager packageManager = context.getPackageManager();
			String packageName = context.getPackageName();
			versionName = packageManager.getPackageInfo(packageName, 0).versionName;
		} catch (NameNotFoundException e) {
		}
		return versionName;
	}

	/**
	 * 比较版本号
	 * 
	 * @param curVersion
	 * @param newVersion
	 * @return
	 */
	public static boolean compareVersion(String curVersion, String newVersion) {
		if (curVersion != null && newVersion != null) {
			String curV[] = curVersion.split("\\.");
			String newV[] = newVersion.split("\\.");

			int curCount = curV.length;
			int newCount = newV.length;

			int lowCount = (curCount >= newCount) ? newCount : curCount;

			for (int i = 0; i < lowCount; i++) {
				String curVTemp = curV[i];
				String newVTemp = newV[i];
				try {
					Integer iCur = Integer.valueOf(curVTemp);
					Integer inew = Integer.valueOf(newVTemp);

					if (iCur < inew) {
						return true;
					} else if (iCur > inew) {
						return false;
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}

			if (newCount > curCount) {
				return true;
			}

		}
		return false;
	}

	/**
	 * 拷贝文件从 asset到sd卡上
	 * 
	 * @Description<功能详细描述>
	 * 
	 * @param assetName
	 * @param dir
	 * @LastModifiedDate：2013-12-24
	 * @author ren_qiujing
	 * @EditHistory：<修改内容><修改人>
	 */
	public static void CopyAssets(Context context, String assetName, String dir) {
		AssetManager am = null;
		am = context.getAssets();
		try {
			InputStream is = am.open(assetName);
			File fileDir = new File(dir);
			if (!fileDir.exists()) {
				fileDir.mkdirs();
			}

			File file = new File(dir + "/" + assetName);
			FileOutputStream fileOutputStream = null;
			if (is != null) {
				fileOutputStream = new FileOutputStream(file);
				byte[] buf = new byte[2048];
				int ch = -1;
				while ((ch = is.read(buf)) != -1) {
					fileOutputStream.write(buf, 0, ch);
				}
			}
			fileOutputStream.flush();
			if (fileOutputStream != null) {
				fileOutputStream.close();
			}

		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
		}

	}
}
